package com.example.howard.picview.Tab2Fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.howard.picview.MyApplication;
import com.example.howard.picview.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Howard on 2016/8/17.
 */
public class Tab2Fragment extends Fragment {

    public MyApplication myApplication;
    public List<Map<String, Object>> imagedata = new ArrayList<>();
    //包含首张图片地址和文件夹名字
    public List<Map<String,Object>> titledata=new ArrayList<>();
    private ListView pathlist;
    public Tab2FragmentAdapter myAdapter;
    public AdapterView.OnItemClickListener listener;

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab2, null);
        pathlist= (ListView) view.findViewById(R.id.pathlist);
        myApplication= (MyApplication) getActivity().getApplication();
        imagedata=myApplication.imagedata;

        getimagepath();
        myAdapter=new Tab2FragmentAdapter(getContext(),titledata);
        pathlist.setAdapter(myAdapter);
        refreshListView();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        imagedata=myApplication.imagedata;
        getimagepath();
        myAdapter.setData(titledata);
        myAdapter.notifyDataSetChanged();

    }

    public void getimagepath(){
        int i;
        int j;
        int k;
        titledata.clear();
        String path;
        String data;
        List <String> list=new ArrayList<>();
        List <String> pathlist=new ArrayList<>();
        List <String> firstimagepath=new ArrayList<>();
//        Map<String,Object> map=new HashMap<String, Object>();
        for (i=0;i<imagedata.size();i++) {

            data = imagedata.get(i).get("URL").toString();
            list.add(data);
        }
        Collections.sort(list);

        for (k=0;k<list.size();k++){
            data=list.get(k);
            path=data.substring(17,data.lastIndexOf("/"));
            if (!pathlist.contains(path)){
                pathlist.add(path);
//                firstimagepath.add(data);
            }

        }
        boolean is;
      for (i=0;i<pathlist.size();i++){
          is=true;
          j=0;
          while (is){
              data = imagedata.get(j).get("URL").toString();
              path=data.substring(17,data.lastIndexOf("/"));
              if (pathlist.get(i).equals(path)){
                  firstimagepath.add(data);
                  is=false;
              }
              j++;
          }

      }

        //获得每个文件夹图片的数量
        int[] num=new int[pathlist.size()];
        for (i=0;i<list.size();i++){
            data=list.get(i);
            path=data.substring(17,data.lastIndexOf("/"));
            num[pathlist.indexOf(path)]++;
        }

        for (j=0;j<pathlist.size();j++){
            Map<String,Object> map=new HashMap<String, Object>();
            map.put("path",pathlist.get(j));
            map.put("title",firstimagepath.get(j));
            map.put("num",num[j]);
            titledata.add(map);
        }
    }
    //刷新ListView
    private void refreshListView() {
        myAdapter.notifyDataSetChanged();
        listener = ClickListener();
        pathlist.setOnItemClickListener(listener);
         }
    //点击响应
    private AdapterView.OnItemClickListener ClickListener() {
        AdapterView.OnItemClickListener clickListener = new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {



                Context context=getContext();
                Intent intent=new Intent();
                intent.setClass(context, FragmentViewActivity.class);
                intent.putExtra("path",titledata.get(arg2).get("title").toString());
                intent.putExtra("top",titledata.get(arg2).get("path").toString());
                startActivity(intent);
            }
        };
        return clickListener;
    }

}